package com.eas.ereceipt.adapter;

import java.util.ArrayList;

import com.eas.ereceipt.R;
import com.eas.ereceipt.bean.Receipt;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListReceiptAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ArrayList<Receipt> receipts;

	public ListReceiptAdapter(Context c, ArrayList<Receipt> data) {
		inflater = LayoutInflater.from(c);
		this.receipts = data;
	}

	@Override
	public int getCount() {
		return receipts.size();
	}

	@Override
	public Object getItem(int pos) {
		
		return receipts.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return receipts.get(pos).getId();
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parrent) {
		Receipt re = receipts.get(pos);
		switch (re.getState()) {
		case Receipt.STATE_OK:
			ReceiptViewHolder holder = new ReceiptViewHolder();
			if (convertView==null||!ReceiptViewHolder.class.isInstance(convertView.getTag())){
				convertView = inflater.inflate(R.layout.row_receipt, parrent, false);
				
				holder.imgThumb = (ImageView) convertView.findViewById(R.id.ivReceiptThumb);
				holder.tvTitle = (TextView) convertView.findViewById(R.id.tvReceiptTitle);
				holder.tvCreateTime = (TextView) convertView.findViewById(R.id.tvReceiptCreateTime);
				holder.tvprice = (TextView) convertView.findViewById(R.id.tvReceiptPrice);
				
				convertView.setTag(holder);
			} else holder = (ReceiptViewHolder) convertView.getTag();
			
			holder.tvTitle.setText(re.getTitle());
			holder.tvCreateTime.setText(re.getCreateTime());
			holder.tvprice.setText(re.getPrice() + "$");
			holder.imgThumb.setImageResource(re.getThumbResId());
			
			break;

		case Receipt.STATE_PROCESSING:
			ReceiptProcessingViewHolder holderProcess = new ReceiptProcessingViewHolder();
			if (convertView==null||!ReceiptProcessingViewHolder.class.isInstance(convertView.getTag())){
				convertView = inflater.inflate(R.layout.row_receipt_processing, parrent, false);
				
				holderProcess.imgThumb = (ImageView) convertView.findViewById(R.id.ivReceiptProcessThumb);
				holderProcess.tvState = (TextView) convertView.findViewById(R.id.tvReceiptState);
				holderProcess.tvCreateTime = (TextView) convertView.findViewById(R.id.tvReceiptProcessCreateTime);
				
				convertView.setTag(holderProcess);
			} else holderProcess = (ReceiptProcessingViewHolder) convertView.getTag();
			
			holderProcess.tvCreateTime.setText(re.getCreateTime());
			holderProcess.imgThumb.setImageResource(re.getThumbResId());
			break;
			
		case Receipt.STATE_UPLOADING:
			ReceiptUploadingViewHolder holderUpload = new ReceiptUploadingViewHolder();
			if (convertView==null||!ReceiptUploadingViewHolder.class.isInstance(convertView.getTag())){
				convertView = inflater.inflate(R.layout.row_receipt_uploading, parrent, false);
				
				holderUpload.imgThumb = (ImageView) convertView.findViewById(R.id.ivReceiptUploadThumb);
				holderUpload.tvState = (TextView) convertView.findViewById(R.id.tvReceiptUploadState);
				holderUpload.tvCreateTime = (TextView) convertView.findViewById(R.id.tvReceiptUploadCreateTime);
				
				convertView.setTag(holderUpload);
			} else holderUpload = (ReceiptUploadingViewHolder) convertView.getTag();
			
			holderUpload.tvCreateTime.setText(re.getCreateTime());
			holderUpload.imgThumb.setImageResource(re.getThumbResId());
			break;
			
		}
		return convertView;
	}
	
	class ReceiptViewHolder{
		TextView tvTitle;
		TextView tvCreateTime;
		ImageView imgThumb;
		TextView tvprice;
	}
	
	class ReceiptProcessingViewHolder{
		TextView tvState;
		TextView tvCreateTime;
		ImageView imgThumb;
	}
	
	class ReceiptUploadingViewHolder{
		TextView tvState;
		TextView tvCreateTime;
		ImageView imgThumb;
	}

}
